<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Blog_category extends DataMapper {

	var $table	= 'blog_categories';
	
	var $has_many = array(
		'posts' => array(
			'class' => 'blog_post',
            'other_field' => 'category',
            'join_self_as' => 'blog_category',
            'join_other_as' => 'blog_post',
            'join_table' => 'r_blog_categories_blog_posts')
    );
    
	function __construct()
	{
		parent::__construct();
	}
	
	function get_category_by_id($id, $locale)
	{
		
		$this->db->select("categories.id, $locale AS label");
		$this->db->from('blog_categories AS categories');
		$this->db->where('categories.id', $id);
		
		$q = $this->db->get();
		
		if($q->num_rows() == 1) {
			return $q->row();
		}
		return NULL;
	}
	
	function count_posts_in_category($c) {
		
		$ci =& get_instance();
		
    	$this->db->select('count(blog_post_id) as number_of_posts');
    	$this->db->from('blog_posts as p');
    	$this->db->join('r_blog_categories_blog_posts as r', 'r.blog_post_id=p.id');
    	$this->db->where('blog_category_id', $c->id);
    	$this->db->where('p.deleted', 0);
    	$this->db->where('p.published', 1);
    	$this->db->where('p.lang', $ci->session->userdata('uilang'));
    	
    	$q = $this->db->get()->result();
    	
    	if($q[0]->number_of_posts > 0) {
    		return $q[0]->number_of_posts;
    	}
    	else return 0;
    }
}

?>
